import { Matrix4, Matrix3, Vector2 } from 'three'

import type { MagizTypes, MagizService } from '../../../types/magizTypes'
import type { StyleTypes } from '../../../types/styleTypes'
import type { StyleParsed } from '../../../types/styleParsed'
import type { Temp } from '../../../types/temp'

export { Matrix4, Matrix3, Vector2, getArea, isClockWise, seededRandom, degToRad }
export type { MagizTypes, MagizService, StyleTypes, StyleParsed, Temp }

// import { ShapeUtils } from 'three/src/extras/ShapeUtils.js'

/** 计算多边形面积 */
function getArea(pts: MagizTypes.XY[]) {
  const n = pts.length
  let a = 0.0
  for (let p = n - 1, q = 0; q < n; p = q++) {
    const p1 = pts[p]!
    const p2 = pts[q]!
    a += p1.x * p2.y - p2.x * p1.y
  }

  return a * 0.5
}

/** 判断多边形方向 */
function isClockWise(pts: MagizTypes.XY[]) {
  return getArea(pts) < 0
}

// import { seededRandom, degToRad } from 'three/src/math/MathUtils.js'

let _seed = 1234567

/** Deterministic pseudo-random float in the interval [ 0, 1 ] */
function seededRandom(seed: number) {
  if (seed !== undefined) _seed = seed

  // Mulberry32 generator
  let t = (_seed += 0x6d2b79f5)
  t = Math.imul(t ^ (t >>> 15), t | 1)
  t ^= t + Math.imul(t ^ (t >>> 7), t | 61)

  return ((t ^ (t >>> 14)) >>> 0) / 4294967296
}

const DEG2RAD = Math.PI / 180

function degToRad(degrees: number) {
  return degrees * DEG2RAD
}
